Xbasic

a5_getCalendarEventDataJSON Function

Syntax

dim jsonData as c = a5_getCalendarEventDataJSON(defn as p)

Arguments

defnPointer

An object that defines the data source, query, and the fields that contain the event data.

typeCharacter

The type of query. Can be 'sql' or 'dbf'.

connectionStringCharacter

The connection string to connect to the database. Can be a named connection or an ad-hoc connection string.

tableNameCharacter

The name of the table that contains the event data or SQL query to retrieve the event information from the database.

startDateCharacter

The start date for events. Specified using the format "YYYY-M-D" where YYYY is four digit year, M is a number representing the month, and D is the day of the month. E.g. '1993-1-1' or '2017-11-10'

endDateCharacter

The end date for events. Specified using the format "YYYY-M-D" where YYYY is four digit year, M is a number representing the month, and D is the day of the month. E.g. '1993-1-31' or '2017-2-8'

eventDateCharacter

The table column or field name from the SQL query that contains the event date.

eventIdCharacter

The table column or field name from the SQL query that contains the event id.

eventNameCharacter

The table column or field name from the SQL query that contains the event name.

eventDescriptionCharacter

The table column or field name from the SQL query that contains the event description.

filterCharacter

An optional filter. Used when specifying a table to fetch the data from.

argumentsXMLCharacter

A string containing XML defining arguments used in the filter. argumentsXML is required if filter contains any arguments.

Returns

jsonDataCharacter

A JSON object of dates with one or more events mapped to each date as an array of objects. Each event entry has the following properties:

idNumeric

The event ID.

nameCharacter

The event name.

detailCharacter

A description of the event.

Description

Used in the UX Component for the Calendar Control to get events for a date range in the required JSON format.

Discussion

The a5_getCalendarEventDataJSON() function can be used to retreive data from a SQL or DBF data source to display in a Calendar control. The Calendar control requires data to be in the following format:

{
    '2012-1-1' : [{id: 1, name: 'event1', detail: 'detail for event 1'}],
    '2012-1-5' : [
        {id: 2, name: 'event2', detail: 'detail for event 2'},
        {id: 3, name: 'event3', detail: 'detail for event 3'}
    ],
    '2012-1-12' : [
        {id: 4, name: 'event4', detail: 'detail for event 4'},
        {id: 5, name: 'event5', detail: 'detail for event 5'},
        {id: 6, name: 'event6', detail: 'detail for event 6'},
        {id: 7, name: 'event7', detail: 'detail for event 7'},
        {id: 8, name: 'event8', detail: 'detail for event 8'},
        {id: 9, name: 'event9', detail: 'detail for event 9'}
    ]
}

where each date is a key with an array of event objects that define an id, name, and detail property.

Example: Data from a Table

Data can be retrieved from a specific table or using a SQL select statement. If the event data is contained in a single table, only the table name needs to be specified in the defn property. For example:

dim jsonData as c
dim defn as p
defn.type = "sql"
defn.connectionString = "::Name::AADemo-Northwind"
defn.tableName = "orders"
defn.startDate = "1994-8-1"
defn.endDate = "1994-8-31"
defn.eventDate = "orderDate"
defn.eventId = "orderId"
defn.eventName = "orderDate"
defn.eventDescription = "orderDate"

jsonData = a5_getCalendarEventDataJSON(defn)

showvar(jsonData)

Example: Using a SQL Select Statement

A SQL select statement can also be used to retrieve event data from a database. In the example below, the event name and description are created by building a concatentad string of data from the orders and customers table:

dim jsonData as c
dim defn as p
defn.type = "sql"
defn.connectionString = "::Name::AADemo-Northwind"

defn.tableName = <<%sql%
SELECT orders.orderId as orderId,
    concatenate(orders.orderId, ': ', customers.companyName) as eventName,
    orders.orderDate as orderDate,
    concatenate('Order: ', orders.orderId, ': ', customers.contactName, ' ', customers.companyName) as eventDescription
FROM orders orders
    INNER JOIN customers customers
        ON orders.customerId = customers.customerId
%sql%

defn.startDate = "1994-8-1"
defn.endDate = "1994-8-31"
defn.eventDate = "orderDate"
defn.eventId = "orderId"
defn.eventName = "eventName"
defn.eventDescription = "eventDescription"

jsonData = a5_getCalendarEventDataJSON(defn)

showvar(jsonData)

Example: Specifying a Filter

You can define an optional filter and arguments used to retrieve data from the data source. In the example below, only orders from customers in France will be retrieved:

dim jsonData as c
dim defn as p
defn.type = "sql"
defn.connectionString = "::Name::AADemo-Northwind"
defn.startDate = "1994-8-1"
defn.endDate = "1994-8-31"
defn.eventDate = "orderDate"
defn.eventId = "orderId"
defn.eventName = "eventName"
defn.eventDescription = "eventDescription"

defn.tableName = <<%sql%
SELECT orders.orderId as orderId,
    concatenate(orders.orderId, ': ', customers.companyName) as eventName,
    orders.orderDate as orderDate,
    concatenate('Order: ', orders.orderId, ': ', customers.contactName, ' ', customers.companyName) as eventDescription
FROM orders orders
    INNER JOIN customers customers
        ON orders.customerId = customers.customerId
%sql%

defn.filter = "customers.country = :whatcountry"
defn.argumentsXML = <<%xml%
<SQLArguments>
    <SQLArgument>
        <Name>whatcountry</Name>
        <Data Type="C">France</Data>
        <IsNull Type="L">0</IsNull>
        <Usage>Input</Usage>
    </SQLArgument>
</SQLArguments>
%xml%

jsonData = a5_getCalendarEventDataJSON(defn)

showvar(jsonData)

You do not need to manually define the XML for the argumentsXML property. You can take advantage of the SQL::Arguments class to generate the XML. Simply add the arguments to a SQL::Arguments object using the Set Method. Once you have defined all the arguments used in your filter, you can get the XML representation of the arguments from the SQL::Arguments XML Property. For example:

dim args as sql::Arguments

args.set("whatcountry","Brazil")
args.set("whatcity","Rio de Janeiro")

defn.argumentsXML = args.XML

? defn.argumentsXML
= <SQLArguments>
    <SQLArgument>
        <Name>whatcountry</Name>
        <Data Type="C">Brazil</Data>
        <IsNull Type="L">0</IsNull>
        <Usage>Input</Usage>
    </SQLArgument>
    <SQLArgument>
        <Name>whatcity</Name>
        <Data Type="C">Rio de Janeiro</Data>
        <IsNull Type="L">0</IsNull>
        <Usage>Input</Usage>
    </SQLArgument>
</SQLArguments>

Limitations

UX Component with Calendar Control Only

See Also